package com.wbq.datastructuredemo.activity;

import com.wbq.datastructuredemo.R;
import com.wbq.datastructuredemo.adapter.MainUIAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener,OnItemLongClickListener{
	private static final String TAG = "MainActivity";
	private GridView gv_main;
	private MainUIAdapter adapter;
	//用来持久化一些配置信息
	private SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
		sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);//会生诚意config的xml文件
		this.gv_main = (GridView) super.findViewById(R.id.gv_main);
		adapter = new MainUIAdapter(this);
		this.gv_main.setAdapter(adapter);
		this.gv_main.setOnItemClickListener(this);
		this.gv_main.setOnItemLongClickListener(this);
	}
	
	/**
	 * 当gridview的条目被点击的时候对应的回调
	 * parent : gridview
	 * view : 当前被点击的条目 Linearlayout
	 * position : 点击条目对应的位置
	 * id:代表的行号
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Log.i(TAG, "点击的位置"+position);
		switch (position) {
		case 0:
			Log.i(TAG, "进入数构基础 ");
			loadMainUI(0);
			break;
		case 2:
			Log.i(TAG, "进入动画演示 ");
			loadMainUI(2);
			break;
		case 4:
			loadMainUI(4);
			break;
		default:
			break;
		}
	}

	/**
	 * 当gridview的条目被点击的时候对应的回调
	 * parent : gridview
	 * view : 当前被点击的条目 Linearlayout
	 * position : 点击条目对应的位置
	 * id:代表的行号
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle("设置");
		builder.setMessage("请输入要更改的名称");
		final EditText et = new EditText(MainActivity.this);
		et.setHint("请输入文本");
		builder.setView(et);
		builder.setPositiveButton("确定",new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String name = et.getText().toString().trim();
				if("".equals(name)){
					Toast.makeText(getApplicationContext(), "内容不能为空", 0).show();
					return ; 
				}else{
					//重新刷新整个gridview
					MainUIAdapter ma = (MainUIAdapter) gv_main.getAdapter();
					ma.notifyDataSetChanged();
					
					Editor editor = sp.edit();
					editor.putString("lost_name_"+position, name);
					//完成数据的提交
					editor.commit();
					
					
				}
			}
		}).setNegativeButton("取消", new OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
			
		});
		builder.create().show();
		return false;
	}
	
	/**
	 * 页面跳转
	 */
	private void loadMainUI(int position){
		if(position == 4){
			Intent intent = new Intent(MainActivity.this,EditActivity.class);
			startActivity(intent);
		}else if(position == 0){
			Intent intent = new Intent(MainActivity.this,BasicActivity.class);
			startActivity(intent);
		}else if(position == 2){
			Intent intent = new Intent(MainActivity.this,CartoonActivity.class);
			startActivity(intent);
		}
	}

}
